package com.ssh.admin.action;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.ssh.common.log.LogFactory;
import com.ssh.common.log.LogName;
import com.ssh.common.util.JsonResult;
import com.ssh.common.util.Page;
import com.ssh.entity.Brand;
import com.ssh.entity.Product;
import com.ssh.entity.SuperMarket;
import com.ssh.manager.BrandManager;
import com.ssh.manager.CompanyManager;
import com.ssh.manager.ProductManager;
import com.ssh.manager.SuperMarketManager;

@Controller
@ParentPackage("admin")
@Scope("prototype")
@Results(value={
@Result(name="success", location="/admin/product/productList.jsp"),
@Result(name="findProductByPage", type="json", params={"root","page","excludeproperties","page\\.curPageNum,page\\.totalPageNum,page\\.totalCount," +
"page\\.numberPerPage,page\\.pageData\\[\\d+\\]\\.id,page\\.pageData\\[\\d+\\]\\.name,page\\.pageData\\[\\d+\\]\\.xiangGui,page\\.pageData\\[\\d+\\]\\.createDate," +
"page\\.pageData\\[\\d+\\]\\.barcode,page\\.pageData\\[\\d+\\]\\.pinLei,page\\.pageData\\[\\d+\\]\\.brand\\.name,page\\.pageData\\[\\d+\\]\\.brand\\.id"}),
@Result(name="updateProduct", type="json", params={"root","jr","excludeproperties","jr\\.code"}),
@Result(name="deleteProduct", type="json", params={"root","page","excludeproperties","page\\.curPageNum"}),
@Result(name="getAllProductByBrand", type="json", params={"root","products","excludeproperties","products\\[\\d+\\]\\.id,products\\[\\d+\\]\\.name"})
})
public class ProductAction extends ActionSupport{
	@Resource(name="superMarketManagerImpl")
	private SuperMarketManager superMarketManager;
	@Resource(name="companyManagerImpl")
	private CompanyManager companyManager;
	@Resource(name="productManagerImpl")
	private ProductManager productManager;
	@Resource(name="brandManagerImpl")
	private BrandManager brandManager;
	
	private Product product;
	private Page page = new Page();
	private JsonResult jr=new JsonResult();
	private String isDesc;
	private SuperMarket superMarket;
	private Brand brand;
	private List<Product> products;
	private String ids;
	
	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public List<Product> getProducts() {
		return products;
	}

	public void setProducts(List<Product> products) {
		this.products = products;
	}

	public Brand getBrand() {
		return brand;
	}

	public void setBrand(Brand brand) {
		this.brand = brand;
	}

	public SuperMarket getSuperMarket() {
		return superMarket;
	}

	public void setSuperMarket(SuperMarket superMarket) {
		this.superMarket = superMarket;
	}

	public Product getProduct() {
		return product;
	}

	public void setProduct(Product product) {
		this.product = product;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public JsonResult getJr() {
		return jr;
	}

	public void setJr(JsonResult jr) {
		this.jr = jr;
	}

	public String getIsDesc() {
		return isDesc;
	}

	public void setIsDesc(String isDesc) {
		this.isDesc = isDesc;
	}

	public String goProductList(){
		
		return "success";
	}
	
	/**
	 * 保存品牌
	 * @return
	 */
	public String saveProduct(){
		brand = brandManager.getBrandById(brand.getId());
		product.setBrand(brand);
		product.setDel(0);
		product.setCreateDate(new Timestamp(System.currentTimeMillis()));
		productManager.saveOrUpdateProduct(product);
		
		brand.setId(null);
		return "success";
	}
	
	/**
	 * 修改商品信息
	 * @return
	 */
	public String updateProduct(){
		try {
			Product product2 = productManager.getProductById(product.getId());
			brand = brandManager.getBrandById(brand.getId());
			
			product2.setBrand(brand);
			product2.setBarcode(product.getBarcode());
			product2.setName(product.getName());
			product2.setPinLei(product.getPinLei());
			product2.setXiangGui(product.getXiangGui());
			
			productManager.saveOrUpdateProduct(product2);
			jr.setCode("y");
		} catch (Exception e) {
			jr.setCode("n");
			LogFactory.getDefaultLog().error("商品修改失败", LogName.API);
			e.printStackTrace();
		}
		return "updateProduct";
	}
	
	/**
	 * 删除品牌
	 * @return
	 */
	public String deleteProduct(){
		try {
			if(ids!=null && !ids.equals("")){
				String[] d = ids.split(",");
				for (int i = 0; i < d.length; i++) {
					product = productManager.getProductById(new Long(d[i]));
					product.setDel(1);
					productManager.saveOrUpdateProduct(product);
				}
			}else{
				product = productManager.getProductById(product.getId());
				product.setDel(1);
				productManager.saveOrUpdateProduct(product);
			}
			
			page.setNumberPerPage(20);
			int _p = (int) page.getCurPageNum();
			Map conditionMap = new HashMap();
			conditionMap.put("productName", product.getName());
			conditionMap.put("brand", brand.getId());
			page.setCurPageNum(1);
			productManager.findProduct(conditionMap, page, isDesc);
			if(_p<=page.getTotalPageNum()){
				page.setCurPageNum(_p);
			}else{
				page.setCurPageNum(_p-1);
			}
			jr.setCode("y");
		} catch (Exception e) {
			jr.setCode("n");
			LogFactory.getDefaultLog().error("商品删除失败", LogName.API);
			e.printStackTrace();
		}
		return "deleteProduct";
	}
	
	/**
	 * 分页查询品牌
	 * @return
	 */
	public String findProductByPage(){
		page.setNumberPerPage(20);
		
		Map conditionMap = new HashMap();
		conditionMap.put("productName", product.getName());
		conditionMap.put("brand", brand.getId());
		
		productManager.findProduct(conditionMap, page, isDesc);
		
		return "findProductByPage";
	}
	
	/**
	 * 根据品牌取出该品牌所有商品
	 * @return
	 */
	public String getAllProductByBrand(){
		products = productManager.getProductsByBrand(brand.getId());
		return "getAllProductByBrand";
	}
}
